﻿using System.Collections.Generic;
using System.ComponentModel;

namespace TK.Socket
{
    /// <summary>
    /// socket config
    /// </summary>
    public class SocketConfig
    {
        /// <summary>
        /// socket ip config list
        /// </summary>
        public List<IPConfig> IPConfigList { get; set; } = new List<IPConfig>();

        /// <summary>
        /// Tcp数据协议
        /// </summary>
        public SocketTcpDataProtocolEnum TcpDataProtocol { get; set; }

        /// <summary>
        /// tcp length-value config;TcpDataProtocol=2有效
        /// </summary>
        public TcpLVConfig TcpLVConfig { get; set; }

        /// <summary>
        /// tcp char separator config;TcpDataProtocol=3有效
        /// </summary>
        public TcpSeparatorConfig TcpSeparatorConfig { get; set; }

        /// <summary>
        /// 发送日志开关;默认关闭false
        /// </summary>
        public bool IsSendLog { get; set; }

        /// <summary>
        /// 接收日志开关;默认关闭false
        /// </summary>
        public bool IsReceiveLog { get; set; }
    }

    /// <summary>
    /// sokcet ip config
    /// </summary>
    public class IPConfig
    {
        /// <summary>
        /// ip:ipv6 or ipv4 or empty is any local address
        /// </summary>
        public string IP { get; set; }

        /// <summary>
        /// 监听端口
        /// </summary>
        public int Port { get; set; }

        /// <summary>
        /// socket protocol:0 tcp and udp;1 tcp;2 udp
        /// </summary>
        public SocketTypeEnum SocketType { get; set; }
    }

    /// <summary>
    /// socket protocol
    /// </summary>
    public enum SocketTypeEnum
    {
        /// <summary>
        /// tcp and udp
        /// </summary>
        [Description("tcp and udp")]
        All = 0,

        /// <summary>
        /// tcp
        /// </summary>
        [Description("tcp")]
        Tcp = 1,

        /// <summary>
        /// udp
        /// </summary>
        [Description("udp")]
        Udp = 2

    }

    /// <summary>
    /// Tcp数据协议
    /// </summary>
    public enum SocketTcpDataProtocolEnum
    {
        /// <summary>
        /// 没有数据协议
        /// </summary>
        [Description("没有数据协议")]
        None = 1,

        /// <summary>
        /// length-value分割
        /// </summary>
        [Description("length-value分割")]
        LV = 2,

        /// <summary>
        /// 字符串分割
        /// </summary>
        [Description("字符串分割")]
        Separator = 3
    }

    /// <summary>
    /// tcp length-value config
    /// </summary>
    public class TcpLVConfig
    {
        /// <summary>
        /// 长度所在位置;一般不设置
        /// </summary>
        public int Offset { get; set; }

        /// <summary>
        /// 摘要:长度占据字节数，1/2/4个字节，默认2
        /// </summary>
        public int Size { get; set; } = 2;

        /// <summary>
        /// 过期时间，超过该时间后按废弃数据处理，默认500ms
        /// </summary>
        public int Expire { get; set; } = 500;
    }

    /// <summary>
    /// tcp 字符串分割
    /// </summary>
    public class TcpSeparatorConfig
    {
        /// <summary>
        /// 粘包分割字节数据（默认0x0D,0x0A）
        /// </summary>
        public byte[] SplitData { get; set; } = new byte[2]{ 13, 10 };

        /// <summary>
        /// 最大缓存待处理数据（字节）,默认1024
        /// </summary>
        public int MaxCacheDataLength { get; set; } = 1024;
    }
}
